package net.imwork.amorgemini.controller;

import net.imwork.amorgemini.core.annotation.Log;
import net.imwork.amorgemini.entity.SysUser;
import net.imwork.amorgemini.entity.mongo.User;
import net.imwork.amorgemini.repository.mongo.MongoUserRepository;
import net.imwork.amorgemini.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by lvbr on 2017-09-06 23:42.
 */
@RestController
@RequestMapping("/user")
public class UserCtrl {
    private static final Logger logger = LoggerFactory.getLogger(UserCtrl.class);

    @Autowired
    UserService userService;

    @Autowired
    MongoUserRepository mongoUserRepository;

    @Log(_class = UserCtrl.class)
    @GetMapping("/getAll")
    public List<SysUser> getAll() {
        logger.info(mongoUserRepository.findAll().toString());
        List<SysUser> users = userService.findAll();
        return users;
    }

    @GetMapping("/findByID")
    public SysUser findByID(Integer id) {
        SysUser user = userService.getOne(id);
        User user1 = new User();
        user1.setName(String.valueOf(Math.random() * 1000000));
        user1.setAge((int) Math.random() * 100);
        mongoUserRepository.insert(user1);
        return user;
    }
}
